# Author: Chris Moyer
from boto.sdb.persist import *
from s3object import *

def init_domain(domain=None):
	"""
	Initilaize the domain, this makes sure the root node
	exists, and creates it if it doesn't. It also sets the domain
	to be used in the Persistance layer.

	@return S3Dir Root node
	"""
	if(domain != None):
		set_domain(domain)
	try:
		root = S3Dir.find(parent=None, name="").next()
	except(StopIteration):
		root = S3Dir(name="")
	return root

def clean_domain(domain=None):
	"""
	Start with a blank domain. Calls init_domain after deleting
	everything in the system.

	@return S3Dir Root node
	"""
	if(domain != None):
		set_domain(domain)

	for f in S3File.find():
		f.delete()
	for d in S3Dir.find():
		d.delete()
	for o in S3Object.find():
		o.delete()

	return init_domain()
